package com.cysion.study.ui.fragment

import android.os.Bundle
import android.view.View
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.image.RoundTransform
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.other.startActivity_ex
import com.cysion.targetfun._subscribe
import com.cysion.study.R
import com.cysion.study.helper.UserCache
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.CommentsActivity
import com.cysion.study.ui.PrivateManageActivity
import com.cysion.study.ui.PrviewEditActivity
import com.cysion.study.ui.TutorOrderActivity
import com.cysion.study.widget.ShowToast
import kotlinx.android.synthetic.main.fragment_private_manage.*

//家教管理
class PrivateManageFragment : BaseFragment(), View.OnClickListener {


    override fun getLayoutId(): Int = R.layout.fragment_private_manage

    override fun initView() {
//        tv_notice.setOnClickListener(this)
        tv_activity_more.setOnClickListener(this)
        tv_push.setOnClickListener(this)
        tv_famly_more.setOnClickListener(this)
        tv_order_more.setOnClickListener(this)
    }


    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tv_activity_more -> {//留言列表
                context.startActivity_ex<CommentsActivity>()
            }
            R.id.tv_push -> {//发布家教
//                context.startActivity_ex<PrivateManageActivity>()
                var bundle = Bundle()
                bundle.putString("type", "1")//type 1新增 2编辑
                context.startActivity_ex<PrviewEditActivity>("key", bundle)
            }
            R.id.tv_famly_more -> {//家教管理
                context.startActivity_ex<PrivateManageActivity>()
            }
            R.id.tv_order_more -> {//家教订单
                context.startActivity_ex<TutorOrderActivity>()
            }
        }
    }

    override fun initData() {
        super.initData()
        loadCommentsData()//获取家长留言

        loadFamilyDatas()//获取家教管理列表信息

        loadFamilyOrder()//家教订单

    }

    private fun loadFamilyOrder() {
        ApiClient.api.getTutorOrderList(UserCache.token,"","1").compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (it.size > 0) {
                        ll_family_order.visibility = View.VISIBLE
                        tv_userName.text=it.get(0).fname
                        tv_num.text = it.get(0).useNumbers + "/" + it.get(0).numbers
                        tv_address.text=it.get(0).address
                        tv_order_content.text=it.get(0).content
                        tv_order_no.text=it.get(0).orderNo
                        tv_time.text=it.get(0).payTime
                        tv_order_price.text="￥"+it.get(0).totalMoney
                        //status:  状态 0.已付款 1.授课同意 2.授课拒绝 3.授课确认中 4.已完成 5.已评价
                        if (it.get(0).status == "0") {
                            tv_status.text = "已付款"
                        } else if (it.get(0).status == "1") {
                            tv_status.text = "待上课"
                        } else if (it.get(0).status == "2") {
                            tv_status.text = "授课拒绝"
                        } else if (it.get(0).status == "3") {
                            tv_status.text = "授课确认中"
                        } else if (it.get(0).status == "4") {
                            tv_status.text = "已完成"
                        } else if (it.get(0).status == "5") {
                            tv_status.text = "已评价"
                        }

                    } else {
                        ll_family_order.visibility = View.GONE
                    }
                }
                _onError {

                    ShowToast(ErrorHandler.handle(it).errorMsg)

                }
            }
    }





    private fun loadFamilyDatas() {
        ApiClient.api.getManageInfo(UserCache.token, "1").compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    if (it.size > 0) {
                        ll_family.visibility=View.VISIBLE
                        tv_pro_title.text = it.get(0).province + it.get(0).city + it.get(0).county
                        tv_pro_content.text = "资格证：" + it.get(0).seniority + " ," + it.get(0).levelName//
                        tv_content.text = it.get(0).content
                        tv_tag2.text = "【" + it.get(0).oneCategoryName + "】  " + "【" + it.get(0).twoCategoryName + "】"
                        tv_price.text = it.get(0).money + "/" + it.get(0).units
                    } else {
                        ll_family.visibility=View.GONE
                    }

                }
                _onError {

                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }

    }



    private fun loadCommentsData() {
        ApiClient.api.getChatList(UserCache.token,"1").compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {


                    if (it.size > 0) {
                        rl_activity.visibility = View.VISIBLE
                        Glide.with(context).load(it.get(0).fromUserHeadImageUrl)
                            .apply(RequestOptions.placeholderOf(R.mipmap.place_holder_big))
                            .apply(RequestOptions.bitmapTransform(RoundTransform(6))).into(iv_1)
                        tv_activity_title.text = it.get(0).fromUserName
                        tv_danwei.text = it.get(0).content
                        tv_comment_time.text = it.get(0).createTime

                    } else {
                        rl_activity.visibility=View.GONE
                    }
                }
                _onError {

                    ShowToast(ErrorHandler.handle(it).errorMsg)
                }
            }
    }

    override fun closeMvp() {
    }

}
